package com.example.shop_car_2208.fragment

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.CheckBox
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.example.shop_car_2208.R
import com.example.shop_car_2208.adapter.CarAdapter
import com.example.shop_car_2208.databinding.FragmentCarBinding
import com.example.shop_car_2208.db.MyDBUtils
import com.example.shop_car_2208.mvvm.BaseFragment
import com.example.shop_car_2208.mvvm.BaseViewFragment
import okhttp3.internal.notify


class CarFragment : BaseFragment<FragmentCarBinding,CarViewModel>() {
    lateinit var carAdapter: CarAdapter
    override fun initData() {
        viewModel.carSuccess.observe(this){
            if (it.code == 200){
                carAdapter.data.clear()
                carAdapter.data.addAll(it.data)
                carAdapter.notifyDataSetChanged()
            }
        }
        viewModel.addSuccess.observe(this){

        }
        viewModel.deleteSuccess.observe(this){
            ToastUtils.showLong(it.message)
        }
    }

    override fun initView() {
        binding.cb.setOnCheckedChangeListener { buttonView, isChecked ->
            //点击全选按钮时发生变化
            binding.cb.setOnClickListener {
                carAdapter.data.forEach {
                    it.isChecked = isChecked
                }
                carAdapter.notifyDataSetChanged()
                totalmaney()
            }
        }
        carAdapter = CarAdapter()
        binding.rv.adapter = carAdapter
        binding.rv.layoutManager = LinearLayoutManager(context)
        viewModel.select()

        carAdapter.addChildClickViewIds(R.id.car_add,R.id.car_sub,R.id.car_delete,R.id.car_cb)
        carAdapter.setOnItemChildClickListener { adapter, view, position ->
            when(view.id){
                R.id.car_add->{
                    //实现数量增加
                    carAdapter.data[position].count++//数据集合中的值发生变化
                    //页面发生变化
                    (carAdapter.getViewByPosition(position,R.id.car_count)as TextView).text = "${carAdapter.data[position].count}"
                    viewModel.AddCar(carAdapter.data[position].goods_id,1)
                }
                R.id.car_sub->{
                    //实现减少---数据中
                    if(carAdapter.data[position].count == 1){
                        ToastUtils.showLong("当前数量为1，不可减少")
                        return@setOnItemChildClickListener
                    }
                    carAdapter.data[position].count--//数据集合中的值发生变化
                    //页面发生变化
                    (carAdapter.getViewByPosition(position,R.id.car_count)as TextView).text = "${carAdapter.data[position].count}"
                    viewModel.AddCar(carAdapter.data[position].goods_id,-1)

                }
                R.id.car_delete->{
                    viewModel.delete(carAdapter.data[position].id.toString())
                    //局部刷新---防止当前页面中的数据选中状态发生变化
                    carAdapter.data.removeAt(position)
                    carAdapter.notifyItemRemoved(position)
                }
                R.id.car_cb->{
                    //将check的值为原来的相反值
                    carAdapter.data[position].isChecked = !carAdapter.data[position].isChecked
                }
            }
            //无论点击了数量加减还是item的删除，item的选中，都需要更新金额变化，下方全选按钮发生变化
            totalmaney()
            checkAll()
        }
        binding.tapay.setOnClickListener {
            val list = mutableListOf<Boolean>()
            carAdapter.data.forEach {
                list.add(it.isChecked)
            }
            if (true in list){
                //存在选中的商品待结算
                //先清空待所有待下单的商品
                MyDBUtils.getDB().getOrderDao().delete()
                //将本次选中的商品生成代下单的订单
                carAdapter.data.forEach {
                    if (it.isChecked){
                        it.status = 0
                        MyDBUtils.getDB().getOrderDao().getAdd(it)
                    }
                }
                ARouter.getInstance().build("/app/order").navigation()
            }else{
                ToastUtils.showLong("至少选中一个商品")
            }
        }
    }

    private fun checkAll() {
        //原来接受所以数据的选中状态
        val list = mutableListOf<Boolean>()
        carAdapter.data.forEach{
            list.add(it.isChecked)
        }
        binding.cb.isChecked = false !in list

    }

    private fun totalmaney() {
        var money = 0//合计金额
        carAdapter.data.forEach{
            if (it.isChecked){
                money+=it.count*it.goods_default_price
            }
        }
        binding.red.text = "合计：$money 元"

    }

    override fun getViewModel(): Class<CarViewModel> {
        return CarViewModel::class.java
    }

    override fun getLayout(): Int {
        return R.layout.fragment_car
    }


}